﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;
using TTCN_QLDiemC3.MyUserControl.UCHienThi;

namespace TTCN_QLDiemC3.MyUserControl.UCHienThi
{
    public partial class UCThemLop : UserControl
    {
        LopBUO lopBUO = new LopBUO();
        MonHocBUO mhBUO = new MonHocBUO();

        int id;
        String id_Lop;

        public UCThemLop()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = lopBUO.HienThiLop();

            showKhoi();
            showKhoaHoc();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtTenLop.Text = "";
            cbxKhoaHoc.Text = "";
            cbxKhoi.Text = "";
        }

        void showKhoi() 
        {
            tbl_Khoi tbl_Khoi = (tbl_Khoi)cbxKhoi.SelectedItem;
            cbxKhoi.DataSource = mhBUO.getKhoi();
            cbxKhoi.DisplayMember = "TenKhoi";
            cbxKhoi.ValueMember = "id_Khoi";
        }

        void showKhoaHoc()
        {
            tbl_KhoaHoc tbl_KH = (tbl_KhoaHoc)cbxKhoaHoc.SelectedItem;
            cbxKhoaHoc.DataSource = lopBUO.getKhoaHoc();
            cbxKhoaHoc.DisplayMember = "KhoaHoc";
            cbxKhoaHoc.ValueMember = "id_KhoaHoc";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if ((txtTenLop.Text == "") || (cbxKhoaHoc.Text == "") || (cbxKhoi.Text == ""))
            {
                MessageBox.Show("Chưa nhập đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                tbl_Lop tbl_lop = new tbl_Lop();
                tbl_lop.id_KhoaHoc = cbxKhoaHoc.SelectedValue.ToString();
                tbl_lop.id_Khoi = cbxKhoi.SelectedValue.ToString();
                tbl_lop.TenLop = txtTenLop.Text;

                if (lopBUO.Insert(tbl_lop))
                {
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = lopBUO.HienThiLop();
                    MessageBox.Show("Thêm lớp thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    UCThemLop a = new UCThemLop();
                    Form form = this.ParentForm;
                    ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
                }
                else
                {
                    MessageBox.Show("Thêm lớp thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        id_Lop = dataGridView1.Rows[id].Cells[0].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void danhSáchLớpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCDanhSachLop a = new UCDanhSachLop();
            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
        }

        private void sửaLớpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCSuaLop a = new UCSuaLop(id_Lop);
            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
        }

        private void xóaLớpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu giáo viên", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (lopBUO.Delete(id_Lop))
                {
                    MessageBox.Show("Xóa lớp thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa lớp thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = lopBUO.HienThiLop();
            }
        }
    }
}
